<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Http\Common\Consumer;
use App\Models\WxUser;
use Illuminate\Support\Facades\Cache;

class HomeController extends Controller
{
    /**
     * 构造函数
     */
    public function __construct(Request $request){
        /*验证微信用户token*/
        $res = $request->all();
        if(isset($res['token'])){
            $Comsumer = new Consumer();
            $Comsumer->check_token($res['token']);
        }else{
            return_value('缺少token参数',4001);
        }
    }

    /**
     * 微信用户状态
     * string token 微信用户令牌
     *
     * @return string $en_res
     * int mobile_status 手机号绑定状态（1已绑定0未绑定）
     * int info_status   资料提交状态（1已完善0未完善）
     */
    public function wx_user_status(Request $request){
        $res = $request->all();
        $token = $res['token'];
        $WxUser = WxUser::where(array('token'=>$token))->select('id','mobile')->first();
        if(isset($WxUser)){
            $id = $WxUser['id'];
            $mobile = $WxUser['mobile'];
            if($mobile == ''){
                $data['mobile_status'] = 0;
            }else{
                $data['mobile_status'] = 1;
            }

            $UserInfo = UserInfo::where(array('wx_user_id'=>$id))->select('id')->first();
            if(isset($UserInfo)){
                $data['info_status'] = 1;
            }else{
                $data['info_status'] = 0;
            }

            return_value('成功',0,$data);
        }else{
            return_value('该用户不存在',402);
        }
    }

    /**
     * 发送短信
     * string  mobile 手机号
     *
     * @return string 返回结果
     */
    public function send_short_msg(Request $request){
        $res = $request->all();

        if(!isset($res['mobile'])){
            return_value('缺少手机号参数',401,'');
        }
        $mobile = $res['mobile'];
        $code = mt_rand(1000,9999);//短信验证码

        //查询短信验证码
        $ShortMsg = Cache::get('verification_mobile:'.$mobile);
        if(isset($ShortMsg)){
            return_value('两分钟只能发送一次短信',403);
        }else{
            /*发送短信*/
            $url = 'https://angelapi.bluemoon.com.cn/moonMall-proxy-web/sms/sendMsgByMobile';//测试
//            $url = 'http://mallapi.bluemoon.com.cn/moonMall-proxy-web/sms/sendMsgByMobile';//正式
            $post_arr = array(
                'content' => '您好，短信验证码：'.$code,
                'myAccount' => 'xfzfw',
                'mobileNo' => $mobile,
                'myProjectName' => '消费者服务号',
                'myRemark' => '电商消费者服务项目调用短信'
            );
            $post_json = json_encode($post_arr,true);
            $res = curl_post_bm($url, $post_json);
            $res = json_decode($res, true);
            if($res['isSuccess'] = false){
                return_value($res['responseMsg'],$res['responseCode']);
            }

            //写入短信验证码缓存 2分钟过期
            Cache::put('verification_mobile:'.$mobile, $code, 2);

            Log::debug('短信验证码：'.$code);
            return_value($res['responseMsg'],0,$code);
        }
    }

    /**
     * 校验短信
     * string  mobile 手机号
     * int     code   短信验证码
     *
     * @return string 返回结果
     */
    public function check_short_msg(Request $request){
        $res = $request->all();

        if(!isset($res['mobile'])){
            return_value('缺少手机号参数',401,'');
        }
        $token = $res['token'];
        $mobile = $res['mobile'];
        $code = $res['code'];

        //查询短信验证码
        $ShortMsg = Cache::get('verification_mobile:'.$mobile);
        if(isset($ShortMsg)){
            if($ShortMsg == $code){
                //更新用户信息
                $info['mobile'] = $mobile;
                $info['updated_at'] = date('Y-m-d H:i:s');
                WxUser::where(array('token'=>$token))->update($info);

                return_value('手机号绑定成功',0);
            }else{
                return_value('短信验证码错误',403);
            }
        }else{
            return_value('短信验证码已过期，请重新获取短信验证码',402);
        }
    }


    /**
     * 创建用户资料
     * string  birthday    生日(年-月-日)
     * string  zhiye       职业
     * string  family_pop  家庭人口数
     * string  baby_num    0-3岁宝宝个数
     *
     * @return string $en_res
     */
    public function create_user_info(Request $request){
        $res = $request->all();

        $token = $res['token'];
        $WxUser = WxUser::where(array('token'=>$token))->select('id')->first();
        if(isset($WxUser)){
            $info['wx_user_id'] = $WxUser['id'];
        }else{
            return_value('该用户不存在',402);
        }
        if(isset($res['birthday'])){
            $info['birthday'] = $res['birthday'];
        }
        if(isset($res['zhiye'])){
            $info['zhiye'] = $res['zhiye'];
        }
        if(isset($res['family_pop'])){
            $info['family_pop'] = $res['family_pop'];
        }
        if(isset($res['baby_num'])){
            $info['baby_num'] = $res['baby_num'];
        }
        $info['created_at'] = date('Y-m-d H:i:s');
        UserInfo::insert($info);

        return_value('用户资料创建成功',0);
    }
}